package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.authority.Party;
import com.example.entity.authority.PartyRoleType;
import com.example.entity.authority.SystemRoleType;
import com.example.entity.authority.SystemRoleTypeRelation;
import com.example.mapp.PartyMapp;
import com.example.mapp.PartyRoleTypeMapp;
import com.example.mapp.SystemRoleTypeMapp;
import com.example.mapp.SystemRoleTypeRelationMapp;
import com.example.service.SystemRoleTypeService;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2018/3/19.
 */
@Service
public class SystemRoleTypeServiceImpl implements SystemRoleTypeService{

    private final static Logger logger = (Logger) LoggerFactory.getLogger(SystemRoleTypeServiceImpl.class);

    @Autowired
    private SystemRoleTypeMapp systemRoleTypeMapp;

    @Autowired
    private SystemRoleTypeRelationMapp systemRoleTypeRelationMapp;

    @Autowired
    private PartyRoleTypeMapp partyRoleTypeMapp;

    @Autowired
    private PartyMapp partyMapp;

    @Override
    @Transactional
    public Map<String, Object> add(SystemRoleType systemRoleType) {
        try {
            SystemRoleType s = systemRoleTypeMapp.queryByRoleTypeName(systemRoleType.getRoleTypeName());
            if(null!=s){
                throw  new Exception(systemRoleType.getRoleTypeName()+"已存在!");
            }
            //添加角色
            systemRoleTypeMapp.add(systemRoleType);
            //添加角色和类型的关系
            if(systemRoleType.getRoleList()!=null&& systemRoleType.getRoleList().size()>0){
                for(String ss:systemRoleType.getRoleList()){
                    SystemRoleTypeRelation ssss = new SystemRoleTypeRelation();
                    ssss.setRoleTypeId(systemRoleType.getId().toString());
                    ssss.setRoleId(Long.parseLong(ss));
                    systemRoleTypeRelationMapp.insert(ssss);
                }
            }
            //添加角色类型和机构的关系
            if(systemRoleType.getPartyList()!=null&& systemRoleType.getPartyList().size()>0){
                for(String ss:systemRoleType.getPartyList()){
                    PartyRoleType ssss = new PartyRoleType();
                    ssss.setRoleTypeId(systemRoleType.getId());
                    ssss.setPartyType(Long.parseLong(ss));
                    partyRoleTypeMapp.insert(ssss);
                }
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    @Transactional
    public Map<String, Object> update(SystemRoleType systemRoleType) {
        try {
            SystemRoleType s = systemRoleTypeMapp.queryById(systemRoleType.getId().toString());
            if(null==s){
                throw  new Exception(systemRoleType.getRoleTypeName()+"不存在!");
            }
            if(!s.getRoleTypeName().equals(systemRoleType.getRoleTypeName())){
                SystemRoleType ss = systemRoleTypeMapp.queryByRoleTypeName(systemRoleType.getRoleTypeName());
                if(null!=ss){
                    throw  new Exception(systemRoleType.getRoleTypeName()+"已存在!");
                }
            }
            //添加角色
            systemRoleTypeMapp.update(systemRoleType);
            //删除角色的关系
            systemRoleTypeRelationMapp.delete(s.getId());
            //删除party的关系
            partyRoleTypeMapp.delete(s.getId());
            //添加角色和类型的关系
            if(systemRoleType.getRoleList()!=null && systemRoleType.getRoleList().size()>0){
                for(String ss:systemRoleType.getRoleList()){
                    SystemRoleTypeRelation ssss = new SystemRoleTypeRelation();
                    ssss.setRoleTypeId(systemRoleType.getId().toString());
                    ssss.setRoleId(Long.parseLong(ss));
                    systemRoleTypeRelationMapp.insert(ssss);
                }
            }
            //添加角色类型和机构的关系
            if(systemRoleType.getPartyList()!=null&& systemRoleType.getPartyList().size()>0){
                for(String ss:systemRoleType.getPartyList()){
                    PartyRoleType ssss = new PartyRoleType();
                    ssss.setRoleTypeId(systemRoleType.getId());
                    ssss.setPartyType(Long.parseLong(ss));
                    partyRoleTypeMapp.insert(ssss);
                }
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    public Map<String, Object> queryList(SystemRoleType systemRoleType) {
        List<SystemRoleType> list =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            list = systemRoleTypeMapp.queryList(systemRoleType);
            map.put("list",list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",map);
    }

    @Override
    public Map<String, Object> deleteById(String id) {
        try {
           systemRoleTypeMapp.deleteById(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    public Map<String, Object> queryById(String id) {
        SystemRoleType systemRoleType = null;
        try {
            systemRoleType = systemRoleTypeMapp.queryById(id);
            //查询角色类型拥有的角色
            List<SystemRoleTypeRelation> list  = systemRoleTypeRelationMapp.queryByRoleTypeId(Long.parseLong(id));
            if(list!=null && list.size()>0){
                List<String> slist = new ArrayList<String>();
                for(int i=0;i<list.size();i++){
                    slist.add(list.get(i).getRoleId().toString());
                }
                systemRoleType.setRoleList(slist);
            }
            //查询角色所属的机构
            List<PartyRoleType> partyList  = partyRoleTypeMapp.queryByRoleType(id);
            if(partyList!=null && partyList.size()>0){
                List<String> slist = new ArrayList<String>();
                for(int i=0;i<partyList.size();i++){
                    slist.add(partyList.get(i).getPartyType().toString());
                }
                systemRoleType.setPartyList(slist);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",systemRoleType);
    }

    @Override
    public Map<String, Object> queryListByPartyType(String partyType) {
        List<SystemRoleType> systemRoleType = new ArrayList<SystemRoleType>();
        try {
            systemRoleType = systemRoleTypeMapp.queryByPartyType(partyType);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",systemRoleType);
    }

    @Override
    public Map<String, Object> queryListByPartyId(String partyId) {
        List<SystemRoleType> systemRoleType = new ArrayList<SystemRoleType>();
        try {
            Party party = partyMapp.queryById(partyId);
            if(party!=null){
                systemRoleType = systemRoleTypeMapp.queryByPartyType(party.getPartyType());
            }else {
                throw new Exception("没有查到机构信息");
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("成功",systemRoleType);
    }
}
